﻿
using NewLife.Remoting;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DocumentFormat.OpenXml.Drawing;
using NetDimension.NanUI;
using NetDimension.NanUI.HostWindow;
using NetDimension.NanUI.Browser;
using WalkingTec.Mvvm.Mvc;

namespace ReceiptPrint
{
    public class MainWindow : Formium
    {
        public static MainWindow Instance { get; private set; }
        NotifyIcon notifyIcon = new NotifyIcon();
        ToolStripMenuItem menu1ToolStripMenuItem = new ToolStripMenuItem();
        ContextMenuStrip contextMenuStrip1 = new ContextMenuStrip() { };
        static MainWindow()
        {
            Instance = new MainWindow();
        }

        public MainWindow()
        {
            this.Title = "打印系统";
            this.WindowState = FormWindowState.Maximized;
            SplashScreen.AutoHide = false;
            SplashScreen.ImageLayout = ImageLayout.None;
            this.Icon = Properties.Resources.print;

            // 
            // contextMenuStrip1
            // 
            this.contextMenuStrip1.ImageScalingSize = new System.Drawing.Size(20, 20);
            this.contextMenuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.menu1ToolStripMenuItem});
            this.contextMenuStrip1.Name = "contextMenuStrip1";
            this.contextMenuStrip1.Size = new System.Drawing.Size(129, 28);
            // 
            // menu1ToolStripMenuItem
            // 
            this.menu1ToolStripMenuItem.Name = "menu1ToolStripMenuItem";
            this.menu1ToolStripMenuItem.Size = new System.Drawing.Size(128, 24);
            this.menu1ToolStripMenuItem.Text = "退出";
            this.menu1ToolStripMenuItem.Click += Menu1ToolStripMenuItem_Click;
            notifyIcon.Icon = Properties.Resources.print;
            notifyIcon.Text = "打印系统";
            notifyIcon.DoubleClick += NotifyIcon_DoubleClick;
            notifyIcon.ContextMenuStrip = this.contextMenuStrip1;
            //notifyIcon.Visible = true;
        }

        private void Menu1ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("确定要退出打印系统吗?", "退出提示", MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                AllowClose = true;
                Application.Exit();
            }
        }

        private void NotifyIcon_DoubleClick(object sender, EventArgs e)
        {
            this.Show();
            notifyIcon.Visible = false;
        }

        public override HostWindowType WindowType => HostWindowType.System;
        // 指定启动 Url
        public override string StartUrl => "http://localhost:5000";

        bool loadSuccess = false;
        private void BrowserLoadEnd(object sender, NetDimension.NanUI.Browser.LoadEndEventArgs e)
        {
            if (e.Frame.IsMain && e.HttpStatusCode == 200)
            {
                if (!loadSuccess)
                {
                    loadSuccess = true;
                    SplashScreen.Hide();
                }
            }
        }

        protected override void OnReady()
        {
            // 注册页面加载完成事件
            LoadEnd += BrowserLoadEnd;
            BeforeClose += MainWindow_BeforeClose;
            //ShowDevTools();
        }
        bool AllowClose = false;
        private void MainWindow_BeforeClose(object sender, FormiumCloseEventArgs e)
        {
            if (MessageBox.Show("确定要退出打印系统吗?", "退出提示", MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                AllowClose = true;
                Application.Exit();
            }
            else
            {
                e.Canceled = true;
            }
        }

        public void ShowMain()
        {
            this.Show();
            notifyIcon.Visible = false;
           
        }
    }
}
